﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Bencoder.Test
{
    [TestFixture]
    class DecoderTest
    {
        [Test(Description="Test decodding a string type")]
        public void DecodeString()
        {
            StringBuilder data = new StringBuilder();
            data.Append("5:teste");
            data.Append("1:s");
            data.Append("0:");
            data.Append("10:jdyrnm3496");
            data.Append("1:e");

            List<Bencoder.Types.BElement> decodedData = null;
            using (Bencoder.Decoder decoder = new Bencoder.Decoder())
            {
                decodedData = decoder.Decode(data.ToString());
            }

            Assert.That(decodedData.Count, Is.EqualTo(5));
            Assert.That((string)(Types.BString)decodedData[0], Is.EqualTo("teste"));
            Assert.That((string)(Types.BString)decodedData[1], Is.EqualTo("s"));
            Assert.That((string)(Types.BString)decodedData[2], Is.EqualTo(""));
            Assert.That((string)(Types.BString)decodedData[3], Is.EqualTo("jdyrnm3496"));
            Assert.That((string)(Types.BString)decodedData[4], Is.EqualTo("e"));
        }

        [Test(Description = "Test decodding an integer type")]
        public void DecodeInteger()
        {
            StringBuilder data = new StringBuilder();
            data.Append("i672534823645823e");
            data.Append("i0e");
            data.Append("i12446e");
            data.Append("i-1576e");
            data.Append("i-3e");

            List<Bencoder.Types.BElement> decodedData = null;
            using (Bencoder.Decoder decoder = new Bencoder.Decoder())
            {
                decodedData = decoder.Decode(data.ToString());
            }

            Assert.That(decodedData.Count, Is.EqualTo(5));
            Assert.That((long)(Types.BInteger)decodedData[0], Is.EqualTo(672534823645823));
            Assert.That((long)(Types.BInteger)decodedData[1], Is.EqualTo(0));
            Assert.That((long)(Types.BInteger)decodedData[2], Is.EqualTo(12446));
            Assert.That((long)(Types.BInteger)decodedData[3], Is.EqualTo(-1576));
            Assert.That((long)(Types.BInteger)decodedData[4], Is.EqualTo(-3));
        }
    }
}
